package libs.yzx.zaza.util;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;

import java.util.ArrayList;

/**
 * Created by yzx on 2016/9/13
 */
public class PermissionUtil {

    private static final ArrayList<String> NORMAL_PERMISSION_LIST = new ArrayList<>(40);
    static{
        NORMAL_PERMISSION_LIST.add(Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS);
        NORMAL_PERMISSION_LIST.add(Manifest.permission.ACCESS_NETWORK_STATE);
        NORMAL_PERMISSION_LIST.add("android.permission.ACCESS_NOTIFICATION_POLICY");
        NORMAL_PERMISSION_LIST.add(Manifest.permission.ACCESS_WIFI_STATE);
        NORMAL_PERMISSION_LIST.add(Manifest.permission.BLUETOOTH);
        NORMAL_PERMISSION_LIST.add(Manifest.permission.BLUETOOTH_ADMIN);
        NORMAL_PERMISSION_LIST.add(Manifest.permission.CHANGE_NETWORK_STATE);
        NORMAL_PERMISSION_LIST.add(Manifest.permission.CHANGE_WIFI_MULTICAST_STATE);
        NORMAL_PERMISSION_LIST.add(Manifest.permission.CHANGE_WIFI_STATE);
        NORMAL_PERMISSION_LIST.add(Manifest.permission.DISABLE_KEYGUARD);
        NORMAL_PERMISSION_LIST.add(Manifest.permission.EXPAND_STATUS_BAR);
        NORMAL_PERMISSION_LIST.add(Manifest.permission.FLASHLIGHT);
        NORMAL_PERMISSION_LIST.add(Manifest.permission.GET_ACCOUNTS);
        NORMAL_PERMISSION_LIST.add(Manifest.permission.GET_PACKAGE_SIZE);
        NORMAL_PERMISSION_LIST.add(Manifest.permission.INTERNET);
        NORMAL_PERMISSION_LIST.add(Manifest.permission.KILL_BACKGROUND_PROCESSES);
        NORMAL_PERMISSION_LIST.add(Manifest.permission.MODIFY_AUDIO_SETTINGS);
        NORMAL_PERMISSION_LIST.add(Manifest.permission.NFC);
        NORMAL_PERMISSION_LIST.add(Manifest.permission.READ_SYNC_SETTINGS);
        NORMAL_PERMISSION_LIST.add(Manifest.permission.READ_SYNC_STATS);
        NORMAL_PERMISSION_LIST.add(Manifest.permission.RECEIVE_BOOT_COMPLETED);
        NORMAL_PERMISSION_LIST.add(Manifest.permission.REORDER_TASKS);
        NORMAL_PERMISSION_LIST.add("android.permission.REQUEST_INSTALL_PACKAGES");
        NORMAL_PERMISSION_LIST.add(Manifest.permission.SET_TIME_ZONE);
        NORMAL_PERMISSION_LIST.add(Manifest.permission.SET_WALLPAPER);
        NORMAL_PERMISSION_LIST.add(Manifest.permission.SET_WALLPAPER_HINTS);
        NORMAL_PERMISSION_LIST.add("android.permission.TRANSMIT_IR");
        NORMAL_PERMISSION_LIST.add("android.permission.USE_FINGERPRINT");
        NORMAL_PERMISSION_LIST.add(Manifest.permission.VIBRATE);
        NORMAL_PERMISSION_LIST.add(Manifest.permission.WAKE_LOCK);
        NORMAL_PERMISSION_LIST.add(Manifest.permission.WRITE_SYNC_SETTINGS);
        NORMAL_PERMISSION_LIST.add("com.android.alarm.permission.SET_ALARM");
        NORMAL_PERMISSION_LIST.add("com.android.launcher.permission.INSTALL_SHORTCUT");
        NORMAL_PERMISSION_LIST.add("com.android.launcher.permission.UNINSTALL_SHORTCUT");
    }


    public static boolean shouldShowRequestPermissionRationale(Activity activity,String per){
        return ActivityCompat.shouldShowRequestPermissionRationale(activity,per);
    }

    public static boolean isNormal(String per){
        return NORMAL_PERMISSION_LIST.contains(per);
    }

    public static PermissionDealer with(Activity activity){
        return new PermissionDealer(activity);
    }

    public static PermissionDealer with(Fragment fragment){
        return new PermissionDealer(fragment);
    }


    //=================================================================//


    public interface OnPermissionRequestListener{
        void onGranted(int requestCode,String[] permissions);
        void onFailed(int requestCode,String[] permissions);
    }


    //=================================================================//


    public static class PermissionDealer {
        private Activity mActivity;
        private Fragment mFragment;
        private OnPermissionRequestListener listener;


        private PermissionDealer(Fragment fragment){
            mFragment = fragment;
        }
        private PermissionDealer(Activity activity){
            mActivity = activity;
        }


        public void handleResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){
            if(listener == null) return ;
            if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) listener.onGranted(requestCode,permissions);
            else listener.onFailed(requestCode,permissions);
        }
        public void setListener(OnPermissionRequestListener listener){
            this.listener = listener;
        }
        public void request( int requestCode , String... ps){
            if(mActivity!=null) ActivityCompat.requestPermissions(mActivity,ps,requestCode);
            else mFragment.requestPermissions(ps,requestCode);
        }
    }

}
